package com.banhujiu.spider.region.factory

import com.banhujiu.spider.region.JSQueue
import com.banhujiu.spider.region.ProvinceQueue
import com.banhujiu.spider.region.enties.JSEntry
import com.banhujiu.spider.region.enties.RegionVo
import org.apache.logging.log4j.LogManager
import org.jsoup.nodes.Element
import java.io.IOException

/**
 * @author banhujiu
 * @date 2017/11/23 0023 10:14
 */
class JsFactory(private val jsQueue: JSQueue, private val provinceQueue: ProvinceQueue) : Runnable {
    private val logger = LogManager.getLogger(this::class.java.name)

    override fun run() {
        try {
            while (!Thread.interrupted()) {
                val take: JSEntry = jsQueue.take()
                val links: ArrayList<Element>
                try {
                    links = take.level.getLinks(take.url)
                } catch (e: IOException) {
                    if (take.errorCount > 2) {
                        logger.error("访问" + take.url + "失败")
                        jsQueue.put(JSEntry(take))
                    }
                    continue
                }
                provinceQueue.put(RegionVo(links, take.level, take.pRegionEntry))
            }
        } catch (e: InterruptedException) {
            logger.info("Interrupted JsFactory")
        }
        logger.info("end JsFactory")
    }
}
